package org.jeecg;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.*;

public class UDP_Client_file {

    public static void main(String[] args) throws FileNotFoundException, SocketException, UnknownHostException {
        try {
            // 创建UDP套接字
            DatagramSocket socket = new DatagramSocket();
            // 要发送的文件
            File file = new File("E:\\1.resource\\1.work\\11.mt\\5.tz\\2.code\\llagent\\net.ini");
            FileInputStream fis = new FileInputStream(file);
            byte[] buffer = new byte[1024];
            int bytesRead;
            InetAddress address = InetAddress.getByName("192.168.2.70");// 接收方IP地址
            int port = 9999; // 接收方端口号

            while ((bytesRead = fis.read(buffer))!= -1) {
                // 创建UDP数据包
                DatagramPacket packet = new DatagramPacket(buffer, bytesRead, address, port);
                // 发送数据包
                socket.send(packet);
            }
            // 发送文件结束标志（这里简单地发送一个空包表示文件结束）
            byte[] endFlag = new byte[0];
            DatagramPacket endPacket = new DatagramPacket(endFlag, 0, address, port);
            socket.send(endPacket);

            fis.close();
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}